package com.example.jbmsuppliers.sinopec.doorjurisd;

import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.jbmsuppliers.R;
import com.example.jbmsuppliers.sinopec.activity.ActivityUse;
import com.example.jbmsuppliers.sinopec.net.NetWork;
import com.example.jbmsuppliers.sinopec.utlis.ExitAppUtlis;
import com.example.jbmsuppliers.sinopec.view.DialogLoading;
import com.jbm.jbmsupplier.util.IEGuardKey;

import butterknife.ButterKnife;
import butterknife.InjectView;
import butterknife.OnClick;

public class EmpowerActivity extends ActivityUse {

    @InjectView(R.id.header_back)
    ImageView headerBack;
    @InjectView(R.id.header_title)
    TextView headerTitle;
    @InjectView(R.id.header_right)
    TextView headerRight;
    @InjectView(R.id.id_empower_mobile)
    EditText idEmpowerMobile;
    private DialogLoading dialog;
    public final static int Cmd_Empowers = 0001;
    private final static int MSG_CODE = 0002;

    @Override
    protected int setContentView1() {
        return R.layout.activity_empower;
    }

    @Override
    protected void initView() {
        ExitAppUtlis.getInstance().addActivity(this);
        ButterKnife.inject(this);
        headerTitle.setText("开门授权");
        headerRight.setText("完成");
    }


    private Handler mHandler = new Handler() {
        public void handleMessage(Message paramMessage) {
            switch (paramMessage.what) {
                case Cmd_Empowers: {
                    IEGuardKey key = (IEGuardKey) paramMessage.obj;
                    if (key == null) {
                        if (dialog != null) {
                            dialog.dismiss();
                        }
                        hint("授权失败");
                        return;
                    } else if (key.back) {
                        if (dialog != null) {
                            dialog.dismiss();
                        }
                        hint("授权成功", mHandler, MSG_CODE);
                        break;
                    }
                }
                case MSG_CODE: {
                    finish();
                    break;
                }
            }
        }
    };


    @OnClick({R.id.header_right, R.id.header_back})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.header_right:
                try {
                    String mobile = idEmpowerMobile.getText().toString();
                    if (mobile.equals("")) {
                        hint("请输入手机号！");
                    } else if (mobile.length() > 11 || mobile.length() > 11) {
                        hint("请输入正确手机号！");
                        return;
                    } else {
                        NetWork network = new NetWork(mHandler);
                        network.Empower(getIntent().getIntExtra("egkey_id", 1), mobile);
                        dialog = new DialogLoading(this, "正在授权...", true);
                        dialog.show();
                    }
                } catch (Exception e) {
                    String msg = e.getMessage();
                }
                break;
            case R.id.header_back:
                finish();
                break;
        }
    }


}
